﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;

namespace MQReceiver2
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            //Fanout();
            //Direct();
            Topic();
        }
        /// <summary>
        /// 不经过交换机，队列点对点
        /// </summary>
        public static void Basic()
        {
            var factory = new ConnectionFactory()
            {
                HostName = "127.0.0.1",
                UserName = "xjl",
                Password = "xjl",
                VirtualHost = "xjl_vh"
            };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: "mymessage",
                                              durable: false,
                                              exclusive: false,
                                              autoDelete: false,
                                              arguments: null);
                var consumer = new EventingBasicConsumer(channel);

                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body.ToArray());
                    Console.WriteLine("收到消息 {0}", message);
                };
                channel.BasicConsume(queue: "mymessage",
                                     autoAck: true,
                                     consumer: consumer);

                Console.WriteLine(" 按回车退出");
                Console.ReadLine();
            }

        }
        /// <summary>
        /// 经过交换机，广播模式
        /// </summary>
        public static void Fanout()
        {
            var factory = new ConnectionFactory()
            {
                HostName = "127.0.0.1",
                UserName = "xjl",
                Password = "xjl",
                VirtualHost = "xjl_vh"
            };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare(exchange: "xjl_exchange", ExchangeType.Fanout, durable: true, autoDelete: false);
                channel.QueueDeclare(queue: "mymessage2",
                                              durable: false,
                                              exclusive: false,
                                              autoDelete: false,
                                              arguments: null);
                channel.QueueBind(queue: "mymessage2", exchange: "xjl_exchange", routingKey: "");
                var consumer = new EventingBasicConsumer(channel);

                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body.ToArray());
                    Console.WriteLine("收到消息 {0}", message);
                };
                channel.BasicConsume(queue: "mymessage2",
                                     autoAck: true,
                                     consumer: consumer);

                Console.WriteLine(" 按回车退出");
                Console.ReadLine();
            }
        }


        /// <summary>
        /// 经过交换机，直连模式
        /// </summary>
        public static void Direct()
        {
            var factory = new ConnectionFactory()
            {
                HostName = "127.0.0.1",
                UserName = "xjl",
                Password = "xjl",
                VirtualHost = "xjl_vh"
            };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare(exchange: "xjl.exchange", ExchangeType.Direct, durable: true, autoDelete: false);
                channel.QueueDeclare(queue: "mymessage2",
                                              durable: false,
                                              exclusive: false,
                                              autoDelete: false,
                                              arguments: null);
                channel.QueueBind(queue: "mymessage2", exchange: "xjl.exchange", routingKey: "email");
                var consumer = new EventingBasicConsumer(channel);

                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body.ToArray());
                    Console.WriteLine("收到消息 {0}", message);
                };
                channel.BasicConsume(queue: "mymessage2",
                                     autoAck: true,
                                     consumer: consumer);

                Console.WriteLine(" 按回车退出");
                Console.ReadLine();
            }
        }



        /// <summary>
        /// 经过交换机，直连模式
        /// </summary>
        public static void Topic()
        {
            var factory = new ConnectionFactory()
            {
                HostName = "127.0.0.1",
                UserName = "xjl",
                Password = "xjl",
                VirtualHost = "xjl_vh"
            };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare(exchange: "amq.topic", ExchangeType.Topic, durable: true, autoDelete: false);
                channel.QueueDeclare(queue: "mymessage2",
                                              durable: false,
                                              exclusive: false,
                                              autoDelete: false,
                                              arguments: null);
                channel.QueueBind(queue: "mymessage2", exchange: "amq.topic", routingKey: "#.email");
                var consumer = new EventingBasicConsumer(channel);

                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body.ToArray());
                    Console.WriteLine("收到消息 {0}", message);
                };
                channel.BasicConsume(queue: "mymessage2",
                                     autoAck: true,
                                     consumer: consumer);

                Console.WriteLine(" 按回车退出");
                Console.ReadLine();
            }
        }
    }
}
